﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace CRM.Models
{
    [Comment("客户公司表")]
    [Index(nameof(CompanyId), Name = "CompanyId")]
    [Index(nameof(OemId), Name = "OemId")]
    [Index(nameof(UserId), Name = "UserId")]
    [Index(nameof(IsExistIntention), Name = "IsExistIntention")]
    [Index(nameof(IsExistMy), Name = "IsExistMy")]
    [Index(nameof(IsExistPrivate), Name = "IsExistPrivate")]
    [Index(nameof(DiyGroupId), Name = "DiyGroupId")]
    [Index(nameof(SourceId), Name = "SourceId")]
    [Index(nameof(NameHash))]
    [Index(nameof(OemId), nameof(UserId), nameof(From), nameof(RelationId))]
    public class CustomerCompany : BaseModel
    {
        private string _url;

        public int Id { get; set; }

        [Comment("全球搜索关联id/营销任务id")]
        public int RelationId { get; set; }

        [MaxLength(255)]
        public string Name { get; set; }
        
        [Comment("标题hash索引")]
        public long NameHash { get; set; }

        public string Desc { get; set; }

        [Comment("数据来源")]
        public FromType From { get; set; }

        public enum FromType
        {
            Other,
            [Description("全球搜索")]
            GlobalSearch,
            [Description("邮件营销")]
            EmailMarketing,
        }

        [Comment("是否存在公海客户")]
        public IsExistPrivateType IsExistPrivate { get; set; }

        public enum IsExistPrivateType
        {
            [Description("否")]
            NO,
            [Description("是")]
            Yes
        }

        [Comment("是否存在意向客户")]
        public IsExistIntentionType IsExistIntention { get; set; }

        public enum IsExistIntentionType
        {
            [Description("否")]
            NO,
            [Description("是")]
            Yes
        }

        [Comment("是否存在我的客户")]
        public IsExistMyType IsExistMy { get; set; } = IsExistMyType.Yes;

        public enum IsExistMyType
        {
            [Description("否")]
            NO,
            [Description("是")]
            Yes
        }

        [MaxLength(255)]
        public string Url
        {
            get => _url;
            set
            {
                _url = value;
                Domain = string.IsNullOrEmpty(value)? null : new Uri(value).Host;
            }
        }

        [MaxLength(100)] public string Domain { get; private set; }


        [Comment("分组")]
        public int? DiyGroupId { get; set; }

        [Comment("来源")]
        public int? SourceId { get; set; } = 0;
        #nullable enable
        public CustomerGroup? DiyGroup { get; set; }
        public CustomerSource? Source { get; set; }
        public virtual List<CustomerLabel>? Labels { get; set; }
        #nullable restore


        public int UserId { get; set; }
        public User User { get; set; }
        public int GroupId { get; set; }
        public int CompanyId { get; set; }
        public int OemId { get; set; }
    }
}
